import * as types from './type'
const state = {
	searchlist: "",
	isshowbox: false,
	songurl: "",

	isplaying: false,
	collect: [],
	loading: false,
	isshowcollect: false,
	playlist:[],
	isshowsearch:true,
	isback:false
};

const mutations = {
	[types.ISBACKFALSE](state) {
		state.isback = false;
	},
	[types.ISBACK](state) {
		state.isback = true;
	},
	[types.ISSHOWSEARCHSHOW](state, obj) {
		state.isshowsearch = true;
	},
	[types.ISSHOWSEARCH](state, obj) {
		state.isshowsearch = false;
	},
	[types.SUGGEST](state, obj) {
		state.searchlist = obj;
	},
	[types.SEARCHLIST](state, obj) {
		state.searchlist = obj;
	},
	[types.AUDIOSHOW](state) {
		state.isshowbox = !state.isshowbox;
	},

	[types.ISPLAYING](state) {
		state.isplaying = true;
	},
	[types.NOPLAYING](state) {
		state.isplaying = false;
	},
	[types.DATAURL](state, obj) {
		state.songurl = obj;
	},
	[types.GETCOLLECT](state, obj) {
		//console.log(obj[0].id)

		function contains(arr, obj) {
			var i = arr.length;
			while(i--) {
				if(arr[i].tid === obj) {
					return true;
				}
			}
			return false;
		}
		if(contains(state.collect, obj[0].id)) {
			mui.toast('您已经添加过了')
			return
		}else{
			mui.toast('已加入我的收藏夹')
		}

		state.collect.push({
			uid:state.collect.length,
			title: obj[0].name,
			
			id: obj[1],
			tid: obj[0].id,
			singer: obj[0].artists[0].name
		})

		localStorage.setItem('collect', JSON.stringify(state.collect))

	},
	[types.ISSHOWCOLLECT](state) {

		state.isshowcollect = !state.isshowcollect;
	},
	[types.REMOVETHIS](state, obj) {

		state.collect.splice(state.collect.indexOf(obj), 1)
		localStorage.setItem('collect', JSON.stringify(state.collect))
	},
	[types.CLEAR](state, obj) {

		state.collect.splice(0, state.collect.length)
		localStorage.setItem('collect', JSON.stringify(state.collect))

	},
	/*loading*/
	[types.HIDE_LOADING](state) {
		state.loading = false;
	},
	[types.SHOW_LOADING](state) {
		state.loading = true;
	},
	
  [types.SET_PLAYLIST](state, list) {
  
    state.playlist = list
  },
}

const getters = {
	isback(state) {
		return state.isback;
	},
	searchlist(state) {
		return state.searchlist;
	},
	isshowbox(state) {
		return state.isshowbox;
	},
	
	isplaying(state) {
		return state.isplaying;
	},
	songurl(state) {
		return state.songurl;
	},

	collect(state) {
		state.collect = JSON.parse(localStorage.getItem('collect') || '[]')
		return state.collect
	},
	isshowcollect(state) {
		return state.isshowcollect;
	},
	loading(state) {
		return state.loading;
	},
	
	playlist(state){
		state.playlist = JSON.parse(localStorage.getItem('collect') || '[]')
		return state.playlist
	},
	isshowsearch(state){
		return state.isshowsearch
	}
};

export default {
	state,
	mutations,
	getters
}